/*
 * @Description: 
 * @Author: xuzp4
 * @Date: 2022-03-22 17:23:06
 * @LastEditors: xuzp4
 * @LastEditTime: 2022-03-22 17:30:26
 * @FilePath: \visitor_management\src\utils\index.js
 */
import {
    Message,
    MessageBox 
} from 'element-ui'
import i18n from '@/lang'
class Util {
    msg = {
        success: (msg) => {
            Message({
                message: i18n.t('common.' + msg),
                type: 'success',
            })
        },
        error: (msg) => {
            Message({
                message: i18n.t('common.' + msg),
                type: 'error',
            })
        },
        warning: (msg) => {
            Message({
                message: i18n.t('common.' + msg),
                type: 'warning',
            })
        },
    }
    confirm = (title,tips,confirmText,cancelText,confirm,cancel) => {
        MessageBox.confirm(tips, title, {
            confirmButtonText: confirmText || i18n.t('common.confirm'),
            cancelButtonText: cancelText || i18n.t('common.cancel'),
            type: 'warning'
          }).then(() => {
            confirm && confirm()
          }).catch(() => {
            cancel && cancel()        
          });
    }
    generateUUID() {
        let dt = new Date().getTime();
        if (window.performance && typeof window.performance.now === "function") {
            dt += performance.now(); // 使用高精度时间戳
        }
        const uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
            const r = (dt + Math.random() * 16) % 16 | 0;
            dt = Math.floor(dt / 16);
            return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
        });
        return uuid;
    }
    formatNumberWithCommas(number) {
        // 将数字转换为字符串，并使用正则表达式和替换方法添加逗号
        return number.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
      }
}

export default new Util()